package  sum;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.animation.AnimationTimer;

public class GameScene {
    private Pane root;
    private ImageView plane;
    private ImageView bird;
    private ImageView landmark;
    private Button captureButton;
    private boolean up, down, left, right;
    private int level;
    private final int MAX_LEVEL = 3;
    private Scene scene;

    public GameScene(Stage stage, int level) {
        this.level = level;
        root = new Pane();
        // 加载地标图片
        landmark = new ImageView();
        setLandmarkImage(level);
        landmark.setFitWidth(800);
        landmark.setFitHeight(600);
        root.getChildren().add(landmark);

        // 移除上一关、下一关按钮
        // 其余初始化和控件保持不变

        // 加载飞机图片（如无plane.png则用矩形代替）
        try {
            plane = new ImageView(new Image("file:assets/plane.png"));
        } catch (Exception e) {
            plane = new ImageView();
            plane.setStyle("-fx-background-color: gray;");
            plane.setFitWidth(60);
            plane.setFitHeight(40);
        }
        plane.setX(100);
        plane.setY(300);
        plane.setFitWidth(60);
        plane.setFitHeight(40);
        root.getChildren().add(plane);

        // 加载鸟动图1
        bird = new ImageView(new Image("file:assets/bird1.gif"));
        bird.setX(700);
        bird.setY(200);
        bird.setFitWidth(90);
        bird.setFitHeight(60);
        bird.setOpacity(0.85);
        root.getChildren().add(bird);

        // 加载鸟动图2（用bird1.gif代替）
        ImageView bird2 = new ImageView(new Image("file:assets/bird2.gif"));
        bird2.setX(100);
        bird2.setY(100);
        bird2.setFitWidth(90);
        bird2.setFitHeight(60);
        bird2.setOpacity(0.85);
        root.getChildren().add(bird2);

        // 加载鸟动图3
        ImageView bird3 = new ImageView(new Image("file:assets/bird3.gif"));
        bird3.setX(300);
        bird3.setY(350);
        bird3.setFitWidth(90);
        bird3.setFitHeight(60);
        bird3.setOpacity(0.85);
        root.getChildren().add(bird3);

        // 加载鸟动图4
        ImageView bird4 = new ImageView(new Image("file:assets/bird4.gif"));
        bird4.setX(Math.random() * 400);
        bird4.setY(100 + Math.random() * 400);
        bird4.setFitWidth(90);
        bird4.setFitHeight(60);
        bird4.setOpacity(0.85);
        root.getChildren().add(bird4);

        // 拍照按钮
        captureButton = new Button("拍照");
        captureButton.setLayoutX(370);
        captureButton.setLayoutY(550);
        captureButton.setVisible(false);
        captureButton.setOnAction(e -> {
            captureButton.setVisible(false);
            System.out.println("拍到鸟啦！");
        });
        root.getChildren().add(captureButton);

        // 添加settings图标按钮
        ImageView settingsIcon = new ImageView(new Image("file:assets/settings.png"));
        settingsIcon.setFitWidth(40);
        settingsIcon.setFitHeight(40);
        settingsIcon.setX(740);
        settingsIcon.setY(20);
        // 可选：添加点击事件
        settingsIcon.setOnMouseClicked(e -> {
            javafx.stage.Stage dialog = new javafx.stage.Stage();
            dialog.initOwner(stage);
            dialog.setTitle("设置");
            javafx.scene.layout.HBox hbox = new javafx.scene.layout.HBox(30);
            hbox.setStyle("-fx-padding: 30; -fx-alignment: center;");
            javafx.scene.control.Button restartBtn = new javafx.scene.control.Button();
            javafx.scene.control.Button exitBtn = new javafx.scene.control.Button();
            javafx.scene.image.ImageView restartIcon = new javafx.scene.image.ImageView(new javafx.scene.image.Image("file:assets/restart.png"));
            restartIcon.setFitWidth(48);
            restartIcon.setFitHeight(48);
            restartBtn.setGraphic(restartIcon);
            restartBtn.setStyle("-fx-background-color: transparent;");
            javafx.scene.image.ImageView closeIcon = new javafx.scene.image.ImageView(new javafx.scene.image.Image("file:assets/close.png"));
            closeIcon.setFitWidth(48);
            closeIcon.setFitHeight(48);
            exitBtn.setGraphic(closeIcon);
            exitBtn.setStyle("-fx-background-color: transparent;");
            hbox.getChildren().addAll(restartBtn, exitBtn);
            dialog.setScene(new javafx.scene.Scene(hbox));
            dialog.setWidth(200);
            dialog.setHeight(140);
            dialog.setResizable(false);
            dialog.show();
            restartBtn.setOnAction(ev -> {
                dialog.close();
                // 重新开始当前关卡
                stage.setScene((new GameScene(stage, level)).getScene());
            });
            exitBtn.setOnAction(ev -> {
                dialog.close();
                // 返回主界面
                MainPage mainPage = new MainPage();
                try {
                    mainPage.start(stage);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            });
        });
        root.getChildren().add(settingsIcon);

        scene = new Scene(root, 800, 600);
        setKeyHandlers(scene);

        AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                movePlane();
                moveBird();
                moveBird2Left(bird2);
                moveBirdGeneric(bird3);
                moveBirdGeneric(bird4);
                checkCapture();
            }
        };
        timer.start();
    }

    public Scene getScene() {
        return scene;
    }

    private void setKeyHandlers(Scene scene) {
        scene.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.W) up = true;
            if (e.getCode() == KeyCode.S) down = true;
            if (e.getCode() == KeyCode.A) left = true;
            if (e.getCode() == KeyCode.D) right = true;
        });
        scene.setOnKeyReleased(e -> {
            if (e.getCode() == KeyCode.W) up = false;
            if (e.getCode() == KeyCode.S) down = false;
            if (e.getCode() == KeyCode.A) left = false;
            if (e.getCode() == KeyCode.D) right = false;
        });
    }

    private void movePlane() {
        double speed = 4;
        if (up && plane.getY() > 0) plane.setY(plane.getY() - speed);
        if (down && plane.getY() < 600 - plane.getFitHeight()) plane.setY(plane.getY() + speed);
        if (left && plane.getX() > 0) plane.setX(plane.getX() - speed);
        if (right && plane.getX() < 800 - plane.getFitWidth()) plane.setX(plane.getX() + speed);
    }

    private void moveBird() {
        bird.setX(bird.getX() + 2); // 向右飞
        if (bird.getX() > 800) {
            bird.setX(-bird.getFitWidth());
            bird.setY(100 + Math.random() * 400);
        }
    }

    // bird2专用：从右往左飞
    private void moveBird2Left(ImageView bird) {
        bird.setX(bird.getX() - 2);
        if (bird.getX() < -bird.getFitWidth()) {
            bird.setX(800);
            bird.setY(100 + Math.random() * 400);
        }
    }

    // bird3通用：从左往右飞
    private void moveBirdGeneric(ImageView bird) {
        bird.setX(bird.getX() + 2);
        if (bird.getX() > 800) {
            bird.setX(-bird.getFitWidth());
            bird.setY(100 + Math.random() * 400);
        }
    }

    private void setLandmarkImage(int level) {
        String bgPath;
        if (level == 1) {
            bgPath = "file:assets/bg-1.jpg";
        } else if (level == 2) {
            bgPath = "file:assets/bg-2.jpg";
        } else if (level == 3) {
            bgPath = "file:assets/bg-3.jpg";
        } else {
            bgPath = "file:assets/bg-1.jpg";
        }
        System.out.println("加载背景图片路径: " + bgPath);
        landmark.setImage(new Image(bgPath));
    }

    private void checkCapture() {
        boolean intersect = plane.getBoundsInParent().intersects(bird.getBoundsInParent());
        captureButton.setVisible(intersect);
    }
} 